import { IQueryParams } from './queryParams'
import { IPagination } from './pagination'
import { EStoreStatus, EPaymentBrand } from '@shared/enums/index'
import { propertyMap } from '@shared/decorators/property-map'
import { Contact } from './contact'
import { User } from './user'
import { Address } from './address'
import { QrChannel } from './acquirer'
import { UpdateReq } from './update'
import { Currency } from './common'
import { RiskControlScheme } from './risk-control'
import { EChannelsSwitcherCategory } from 'app/routes/store/components/channels-switcher/channels-switcher.interface'
import { IMCC } from '@shared/components/mcc-select/mcc-select.interface'

export interface IStoreQueryParams extends IQueryParams {
  /*
    创建时间
    门店编号
    门店名称
    国家/地区
    门店交易币种
    开通渠道
    机构编号
    机构名称
    集团编号
    集团名称
    商户编号
    商户名称
  */
  startTime?: string
  endTime?: string
  storeNum?: string
  storeName?: string

  /** 用于导出报表 */
  intStoreCode?: string
}

export interface IStoreDetailQueryParams {
  intStoreCode: string
}

export class StoreRiskControl {
  schemeCode: string
}

export class StoreCommon {
  /* new */
  insCode: string // 机构编号
  insName: string // 机构名称
  insNameEN: string // 机构英文名称
  agentCode: string // 代理编号
  agentName: string // 代理名称
  agentNameEN: string // 代理英文名称
  intMerCode: string // 内部商户号
  merNum: string // 商户编号
  merName: string // 商户名称
  merNameEN: string // 商户英文名称
  intStoreCode: string // 内部门店号
  storeNum: string // 门店编号
  storeName: string // 门店名称
  storeNameEN: string // 门店英文名称
  multiTransCurrConfig: string[] // 门店交易币种数组，多币种需求
  remark?: string // 备注信息
  permission: string[] // 接口权限
  paymentBrands: EPaymentBrand[] // 支付品牌
  contact: Contact // 联系信息
  address: Address // 地址信息
  mcc: string
  mccEnName?: string
  @propertyMap('storeStatus')
  status: EStoreStatus // 门店状态，对应服务端字段为 merStatus

  /* old */
  detail: StoreDetail
  options: IStoreOptions
  alpRefundAsync: boolean // true表示异步退款，false 表示同步退款
  isCollectQRCodeAmountFixed: boolean // 是否支持固定金额收款码
  drivers?: IDriver[]
  isSupportTips: boolean // 是否支持小费
  tipsRatios: number[] // 小费比率

  @propertyMap('createTime')
  createdAt: string
  @propertyMap('updateTime')
  updatedAt: string
  currencies: Currency[]

  settlement: Settlement
  riskControl: RiskControlScheme

  constructor() {
    this.settlement = new Settlement()
    this.riskControl = new RiskControlScheme()
    this.contact = new Contact()
    this.address = new Address()
  }
}

// 门店一些详情的接口
export class StoreDetail {
  @propertyMap('storeNameEN')
  storeName: string // 门店（英文）名称
  @propertyMap('storeName')
  storeNameLocal: string // 门店本地名称
  nation: string
  province: string
  city: string
  addr: string
  commodityName: string
  mcc: string
  contact: string
  email: string

  constructor() {}
}

// 一些需要送往渠道的接口
export interface IStoreOptions {
  merchant_name?: string
  merchant_no?: string
  store_no?: string
  business_no?: string
  terminal_id?: string
  mcc?: string
  region_code?: string
}

// 门店中司机信息
export interface IDriver {
  driverName: string // 名称
  driverContact: string // 联系电话
  driverAccount: string // 编号
}

// 渠道交易信息
// export class TransChannel {
//   public intStoreCode: string // 内部门店号
//   public chanCode: string // 渠道代码
//   public cardBrand: string // 卡品牌
//   public mcc: string // 交易MCC
//   public currency: string // 交易币种
//   public fee: number // 交易费率
//   public chanMerID: string // 大商户号
//   public chanMerName: string // 大商户名称
//   public secondaryChanMerID: string // 子商户号
//   public secondaryChanMerName: string // 子商户名称
// }

// 清算信息
export class Settlement {
  public bank: string // 开户银行
  public bankCodeSWIFT: string // 开户银行SWIFT代码
  public bankAccount: string // 银行账号
  public bankAccountName: string // 银行账号名
  public isSupportDualMessage = false // 是否支持双信息
  public settleType: ESettleType = ESettleType.T1 // 清算类型，1: T+1, 2: 延迟清算, 3: 累计清算
  public settleCycleDays = 1 // 延迟/累计天数
  public settleTime: ESettleTime | SettleTime// 结算时间
}
export class SettleTime {
  public clockFrom: string
  public clockTo: string
  public tradeDayFrom = 'T-1'
  public tradeDayTo = 'T'
  constructor(clockFrom: string, clockTo: string) {
    this.clockFrom = clockFrom
    this.clockTo = clockTo
  }
}

export enum ESettleType {
  T1 = '1',
  Delay = '3',
  AddUp = '4',
}

export enum ESettleTime {
  GMT01 = 'GMT01',
  GMT02 = 'GMT02',
  GMT03 = 'GMT03',
  GMT04 = 'GMT04',
  GMT05 = 'GMT05',
  GMT06 = 'GMT06',
  GMT07 = 'GMT07',
  GMT08 = 'GMT08',
  GMT09 = 'GMT09',
  GMT10 = 'GMT10',
  GMT11 = 'GMT11',
  GMT12 = 'GMT12',
  GMT13 = 'GMT13',
  GMT14 = 'GMT14',
  GMT15 = 'GMT15',
  GMT16 = 'GMT16',
  GMT17 = 'GMT17',
  GMT18 = 'GMT18',
  GMT19 = 'GMT19',
  GMT20 = 'GMT20',
  GMT21 = 'GMT21',
  GMT22 = 'GMT22',
  GMT23 = 'GMT23',
  GMT24 = 'GMT24',
}

export const SettleTimeMap = {
  GMT03 : '03:00(T-1)-03:00(T) (GMT+7)',
  GMT04 : '04:00(T-1)-04:00(T) (GMT+7)',
  GMT05 : '05:00(T-1)-05:00(T) (GMT+7)',
  GMT06 : '06:00(T-1)-06:00(T) (GMT+7)',
  GMT07 : '07:00(T-1)-07:00(T) (GMT+7)',
  GMT08 : '08:00(T-1)-08:00(T) (GMT+7)',
  GMT09 : '09:00(T-1)-09:00(T) (GMT+7)',
  GMT10 : '10:00(T-1)-10:00(T) (GMT+7)',
  GMT11 : '11:00(T-1)-11:00(T) (GMT+7)',
  GMT12 : '12:00(T-1)-12:00(T) (GMT+7)',
  GMT13 : '13:00(T-1)-13:00(T) (GMT+7)',
  GMT14 : '14:00(T-1)-14:00(T) (GMT+7)',
  GMT15 : '15:00(T-1)-15:00(T) (GMT+7)',
  GMT16 : '16:00(T-1)-16:00(T) (GMT+7)',
  GMT17 : '17:00(T-1)-17:00(T) (GMT+7)',
  GMT18 : '18:00(T-1)-18:00(T) (GMT+7)',
  GMT19 : '19:00(T-1)-19:00(T) (GMT+7)',
  GMT20 : '20:00(T-1)-20:00(T) (GMT+7)',
  GMT21 : '21:00(T-1)-21:00(T) (GMT+7)',
  GMT22 : '22:00(T-1)-22:00(T) (GMT+7)',
  GMT23 : '23:00(T-1)-23:00(T) (GMT+7)',
  GMT24 : '24:00(T-1)-24:00(T) (GMT+7)',
  GMT01 : '01:00(T)-01:00(T+1) (GMT+7)',
  GMT02 : '02:00(T)-02:00(T+1) (GMT+7)',
}

export const SettleTypeMap = {
  1: 'BIZ.STORE.OTHER.SETTLEMENT.TYPE.T1',
  3: 'BIZ.STORE.OTHER.SETTLEMENT.TYPE.DELAY',
  4: 'BIZ.STORE.OTHER.SETTLEMENT.TYPE.ADDUP',
}

export interface ISettlementQueryParams {
  bank: string
}

export interface IStorePagination extends IPagination {
  data: Store[]
}

export interface IRouteOfStore {
  id?: string
  chanCode: EPaymentBrand // 渠道代码
  paymentBrand: EPaymentBrand // 支付品牌
  chanMerName?: string
  mcc?: string | IMCC // 交易MCC
  mccNameEN?: string // 交易MCC描述
  fee?: number // 商户费率
  currency?: string // 渠道交易币种
  chanMerObjId?: string
  sponsorMerCode?: string
}

// 新的门店路由与渠道信息保存接口
export interface IQRRouteOfStore extends IRouteOfStore {
  category?: EChannelsSwitcherCategory.QR
  isCreateSubMer?: boolean // 是否自动入网
  chanMerParams?: IStoreChanMer
  payNearby?: boolean
  authOnline?: boolean
  chanMerID?: string
  shopID?: string
}

export interface ICardRouteOfStore extends IRouteOfStore {
  category?: EChannelsSwitcherCategory.Card
  offlineChannelInsCode?: string
  offlineInsCode?: string
  chanMerParams?: IStoreCardChanMer
  ain?: string
  first3DigitsOfSENumber?: string
  chanNameEN?: string
  chanAddrEN?: string
  accessMode?: string
  mna?: string
  sponsorMerCode?: string
  mccNameEN?: string
  replaceMcc?: IMCC
}

export interface IStoreCardChanMer {
  offlineInsCode?: string
  id?: string
  bin?: string
  ica?: string
  ain?: string
  fin?: string
  first3DigitsOfSENumber?: string
  acquiringIIN?: string
}

export interface IStoreCardChanMerMCC extends IStoreCardChanMer {
  ica: string
  first3DigitsOfSENumber?: string
}

export interface IStoreCardChanMerAME extends IStoreCardChanMer {
  ain: string
}

export interface IStoreChanMer {
  paymentBrand?: EPaymentBrand // 支付品牌
  shopID?: string
  payNearby?: boolean
  authOnline?: boolean
}

export interface IStoreChanMerAlipay extends IStoreChanMer {
  PID: string
  secondaryMerchantID: string
  secondaryMerchantName: string
  storeID: string
  storeName: string
}

export interface IStoreChanMerWeChat extends IStoreChanMer {
  merchantID: string
  subMerchantID: string
}

export interface IStoreChanMerUPI extends IStoreChanMer {
  acquiringIIN: string
  forwardingIIN: string
}

export interface IStoreChanMerTRU extends IStoreChanMer {
  shopID: string
  payNearby: boolean
  authOnline: boolean
}

export interface IStoreChannelInAPP {
  appID: string
  signKey?: string
}

export interface IStoreChannelMiniProgram {
  appID: string
  signKey?: string
}

export class StoreQRFeature {
  isRefundAsync = false
  isSupportTips = false
}

export class StoreCardFeature {
  isRefundAsync = false
  tradeTypes: string[] = []
}

// Store represents a store entity
export class Store {
  common: StoreCommon
  qr: StoreQRFeature
  card: StoreCardFeature
  version: number
  createdAt: string
  updatedAt: string
  constructor() {
    this.common = new StoreCommon()
    this.qr = new StoreQRFeature()
    this.card = new StoreCardFeature()
  }
}

/**
 * find 返回
 */
export class StoreFull {
  store: Store
  qrChannels: IQRRouteOfStore[] = []
  cardChannels: ICardRouteOfStore[] = []
  // ecChannels: IECRouteOfStore[] = []
  ecPayChannels: IECRouteOfStore[] = []
  riskScheme: RiskControlScheme // 事中风控方案
  postRiskScheme: RiskControlScheme // 事后风控方案
}

export class StoreCommonReq {
  /* 创建过程中暂存的数据，在回退时需要用到，但不需要发送到后端 */
  insCode: string // 机构号
  insName: string // 机构名称
  insNameEN: string // 机构英文名称
  agentCode: string // 集团号
  agentName: string // 集团名称
  agentNameEN: string // 集团英文名称
  intMerCode: string
  merNum: string
  merName: string
  merNameEN: string
  zipCode: string
  storeNum: string
  storeName: string
  storeNameEN: string
  mcc: string | IMCC
  settlement: Settlement
  contact: Contact
  address: Address // 二期
  currencies: Currency[] = []
  riskControl: StoreRiskControl

  constructor() {
    this.contact = new Contact()
    this.address = new Address()
    this.settlement = new Settlement()
    this.riskControl = new StoreRiskControl()
  }
}

export class StoreReq {
  common: StoreCommonReq
  qr: StoreQRFeature
  card: StoreCardFeature

  constructor() {
    this.common = new StoreCommonReq()
    this.qr = new StoreQRFeature()
    this.card = new StoreCardFeature()
  }
}

/**
 * 新增
 */
export class StoreCreate {
  store: StoreReq
  qrChannels: IQRRouteOfStore[] = []
  cardChannels: ICardRouteOfStore[] = []
  ecPayChannels: IECRouteOfStore[] = []
  users: User[] = []
  riskSchemeCode: string // 事中风控代码
  postRiskSchemeCode: string // 事后风控代码
  selectedPostRiskScheme: PostRiskScheme
  constructor() {
    this.store = new StoreReq()
  }
}

export class StoreCommonUpdate {
  storeName: string // 门店名称
  storeNameEN: string // 门店英文名称
  mcc: string | IMCC
  currencies: Currency[] // 交易币种
  contact: Contact // 联系信息
  address: Address // 地址信息
  @propertyMap('storeStatus')
  status: EStoreStatus // 门店状态，对应服务端字段为 merStatus

  constructor() {
    this.contact = new Contact()
    this.address = new Address()
  }
}

// StoreUpdate represent a request body for updating base info of a store
export class StoreUpdate extends UpdateReq {
  common: StoreCommonUpdate

  constructor(version: number) {
    super()
    this.version = version
    this.common = new StoreCommonUpdate()
  }
}

export class StoreSettlementUpdate extends UpdateReq {
  settlement: Settlement

  constructor(version: number) {
    super()
    this.version = version
    this.settlement = new Settlement()
  }
}

export class StoreRiskControlUpdate extends UpdateReq {
  riskSchemeCode: string
  postRiskSchemeCode: string

  constructor(version: number) {
    super()
    this.version = version
  }
}

export class StoreCardFeatureUpdate extends UpdateReq {
  feature: StoreCardFeature

  constructor() {
    super()
    this.feature = new StoreCardFeature()
  }
}

export class StoreQRFeatureUpdate extends UpdateReq {
  feature: StoreQRFeature

  constructor(version: number) {
    super()
    this.version = version
    this.feature = new StoreQRFeature()
  }
}

export interface IStoreReportPagination {
  url: string
}

export const enum EOtherDetailTab {
  Terminal = 'terminal',
  QrChannel = 'qrChannel',
  CardChannel = 'cardChannel',
  EcChannel = 'ecChannel',
  System = 'system',
  Settlement = 'settlement',
  QrFunction = 'qrFunction',
  CardFunction = 'cardFunction',
  RiskControl = 'riskControl',
  Users = 'users',
  Rules = 'rules',
  Range = 'range',
  Merchant = 'merchant',
}

// AMEX 渠道

export class MetaData {
  chanNameEN?: string
  chanAddrEN?: string
}

export interface IECRouteOfStore extends IRouteOfStore {
  category?: EChannelsSwitcherCategory.EC
  chanMerParams?: IStoreECChanMer
}

export interface IStoreECChanMer {
  upiAcquiringIIN?: string
  upiMerchantID?: string
  upiMerchantAbbreviation?: string
  fee?: number
}

export class PostRiskScheme {
  schemeCode?: string
  schemeName?: string
  schemeDesc?: string
}

export class Generadata {
  value: number
}
